package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Depot;
import cn.wolfcode.crm.query.KeywordsQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IDepotService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("depot")
public class DepotController {
    @Autowired
    private IDepotService depotService;

    @RequestMapping("")
    public String list(){
        return "depot/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(KeywordsQueryObject qo){
        return depotService.query(qo);
    }


    @RequestMapping("queryList")
    @ResponseBody
    public List<Depot> queryList(){
        return depotService.list();
    }


    @RequestMapping("delete")
    @ResponseBody
    public JsonResult list(Long id){
        JsonResult result = new JsonResult();
        try {
           depotService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Depot entity){
        JsonResult result = new JsonResult();
        try {
            depotService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Depot entity){
        JsonResult result = new JsonResult();
        try {
            depotService.insert(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }
}
